package com.qf.listener;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainManager;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 *
 * 监听消息
 * @author lixu
 */
@Component
public class RabbitMQListener {


    @Autowired
    private FilterChainManager filterChainManager;

    /**
     * 监听接收 接口模块发送过来的消息
     *
     *        //队列
     *             //public final static String TOPIC_PRE_SEND = "pre_send_sms_topic";
     *             //交换机名称
     *             //public final static String TOPIC_PRE_SEND = "pre_send_sms_topic_exchange";
     *
     *             "routingKey",
     *
     *             第三种方式： 注解来创建交换机创建队列并绑定
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMQConstants.TOPIC_PRE_SEND,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = RabbitMQConstants.TOPIC_PRE_SEND+"_exchange"),
            key = "routingKey"
    ))
    public void receiverStandardSubmit(StandardSubmit standardSubmit, Message message, Channel channel){
        try {
            System.out.println(standardSubmit.getSequenceId());

            //过滤器开始
            filterChainManager.executeFilter(standardSubmit);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            //
        }
    }
}
